import axios from 'axios'
import { useRouter } from 'vue-router'
import { MessageBox, Message, ElMessage } from 'element-plus'
export function request(config) {
    const router = useRouter()
    this.isajax = true
    let token = localStorage.getItem('token')
        // let baseURL = 'http://192.168.0.71:8030'
    let baseURL = 'http://caijin.huanxingbi.cn'

    localStorage.baseURL = baseURL
        //创建axios的实例
    const instance = axios.create({
        // baseURL: '/api',
        baseURL,
        timeout: 5000,
        method: 'POST',
        headers: {
            // 'Content-Type': 'multipart/form-data',
            'token': token
        },
    })

    //axios的拦截器
    //请求拦截
    instance.interceptors.request.use(config => {
        return config;
    })

    instance.interceptors.response.use(res => {

        if (res.data.ret == 455) {
            localStorage.removeItem("token")
            if (this.isajax) {
                ElMessage.error('登录已过期');
                this.isajax = false
            }
            setTimeout(function() {
                router.push({ name: 'login' })
            }, 1500)
            return false
        } else {
            this.isajax = true
            return res.data
        }
    }, err => {
        console.log(err)
        ElMessage.error('服务器异常');

    })


    return instance(config)
}